package com.zhengqiangblog.core;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.zhengqiangblog.blog.admin.AdminAccountAction;
import com.zhengqiangblog.blog.admin.AdminAction;
import com.zhengqiangblog.blog.admin.AdminArticleAction;
import com.zhengqiangblog.blog.admin.form.ArticleForm;
import com.zhengqiangblog.blog.admin.form.LoginForm;
import com.zhengqiangblog.blog.web.AboutAction;
import com.zhengqiangblog.blog.web.ArticleAction;
import com.zhengqiangblog.blog.web.BlogAction;
import com.zhengqiangblog.blog.web.IndexAction;
import com.zhengqiangblog.blog.web.TagsAction;
import com.zhengqiangblog.blog.web.form.PageForm;

public class SimpleMappingConfigs implements MappingConfigs {

	private static final Logger logger = Logger.getLogger(SimpleMappingConfigs.class);

	private static final String VIEW_PREFIX = "/WEB-INF/views/";

	public SimpleMappingConfigs() {
		logger.info("SimpleMappingConfigs initializing...");
	}

	@Override
	public Map<String, NamespaceMapping> configure() {
		Map<String, NamespaceMapping> namespaces = new HashMap<String, NamespaceMapping>();

		// 主页
		NamespaceMapping nsIndex = new NamespaceMapping("/");
		ActionMapping amIndex = new ActionMapping();
		amIndex.setPath("/").setAction(IndexAction.class);
		amIndex.setActionForm(PageForm.class);
		amIndex.addResult(Result.HOME, VIEW_PREFIX + "index.jsp");
		nsIndex.addAction(amIndex);
		namespaces.put(nsIndex.getPath(), nsIndex);

		// 博客页
		NamespaceMapping nsBlog = new NamespaceMapping("/blog");
		ActionMapping amBlog = new ActionMapping();
		amBlog.setPath("/").setAction(BlogAction.class);
		amBlog.setActionForm(PageForm.class);
		amBlog.addResult("BLOG", VIEW_PREFIX + "blog.jsp");
		nsBlog.addAction(amBlog);

		// 博客页 - 分类文章
		ActionMapping amCategory = new ActionMapping();
		amCategory.setPath("/category/{categoryId}").setAction(BlogAction.class).setActionMethod("category");
		amCategory.setActionForm(PageForm.class);
		amCategory.addResult("BLOG", VIEW_PREFIX + "blog.jsp");
		nsBlog.addAction(amCategory);

		// 博客页 - 存档文章
		ActionMapping amArchive = new ActionMapping();
		amArchive.setPath("/archive/{year}/{month}").setAction(BlogAction.class).setActionMethod("archive");
		amArchive.setActionForm(PageForm.class);
		amArchive.addResult("BLOG", VIEW_PREFIX + "blog.jsp");
		nsBlog.addAction(amArchive);

		// 博客页 - 标签文章
		ActionMapping amTag = new ActionMapping();
		amTag.setPath("/tag/{tagName}").setAction(BlogAction.class).setActionMethod("tag");
		amTag.setActionForm(PageForm.class);
		amTag.addResult("BLOG", VIEW_PREFIX + "blog.jsp");
		nsBlog.addAction(amTag);

		// 博客页 - 文章
		ActionMapping amArticle = new ActionMapping();
		amArticle.setPath("/article/{id}").setAction(ArticleAction.class);
		amArticle.addResult("ARTICLE", VIEW_PREFIX + "article.jsp");
		nsBlog.addAction(amArticle);

		namespaces.put(nsBlog.getPath(), nsBlog);

		// 标签页
		NamespaceMapping nsTags = new NamespaceMapping("/tags");
		ActionMapping amTags = new ActionMapping();
		amTags.setPath("/").setAction(TagsAction.class);
		amTags.addResult("TAGS", VIEW_PREFIX + "tags.jsp");
		nsTags.addAction(amTags);
		namespaces.put(nsTags.getPath(), nsTags);

		// 关于页
		NamespaceMapping nsAbout = new NamespaceMapping("/about");
		ActionMapping amAbout = new ActionMapping();
		amAbout.setPath("/").setAction(AboutAction.class);
		amAbout.addResult("ABOUT", VIEW_PREFIX + "about.jsp");
		nsAbout.addAction(amAbout);
		namespaces.put(nsAbout.getPath(), nsAbout);

		// 登录页
		NamespaceMapping nsLogin = new NamespaceMapping("/login");
		ActionMapping amInitLogin = new ActionMapping();
		amInitLogin.setPath("/").setAction(AdminAccountAction.class);
		amInitLogin.setMethod(RequestMethod.GET);
		amInitLogin.addResult("LOGIN", VIEW_PREFIX + "login.jsp");
		nsLogin.addAction(amInitLogin);

		ActionMapping amLogin = new ActionMapping();
		amLogin.setPath("/auth").setAction(AdminAccountAction.class).setActionMethod("login");
		amLogin.setMethod(RequestMethod.POST);
		amLogin.setActionForm(LoginForm.class);
		amLogin.addResult("LOGIN", VIEW_PREFIX + "login.jsp");
		amLogin.addResult("ADMIN_HOME", "/admin/", ResultType.REDIRECT);
		nsLogin.addAction(amLogin);

		namespaces.put(nsLogin.getPath(), nsLogin);

		// 站点管理
		NamespaceMapping nsAdmin = new NamespaceMapping("/admin");
		ActionMapping amAdmin = new ActionMapping();
		amAdmin.setPath("/").setAction(AdminAction.class);
		amAdmin.addResult("ADMIN_HOME", VIEW_PREFIX + "admin/home.jsp");
		nsAdmin.addAction(amAdmin);

		// 文章列表
		ActionMapping amArticleList = new ActionMapping();
		amArticleList.setPath("/article").setAction(AdminArticleAction.class);
		amArticleList.setActionForm(PageForm.class);
		amArticleList.addResult("ADMIN_ARTICLE_LIST", VIEW_PREFIX + "admin/article_list.jsp");
		nsAdmin.addAction(amArticleList);

		// 新建文章 - 初始化
		ActionMapping amArticleAddInit = new ActionMapping();
		amArticleAddInit.setPath("/article/add").setAction(AdminArticleAction.class).setActionMethod("addInit");
		amArticleAddInit.setMethod(RequestMethod.GET);
		amArticleAddInit.addResult("ADMIN_ARTICLE_ADD", VIEW_PREFIX + "admin/article_add.jsp");
		nsAdmin.addAction(amArticleAddInit);

		// 编辑文章 - 初始化
		ActionMapping amArticleEditInit = new ActionMapping();
		amArticleEditInit.setPath("/article/edit/{id}").setAction(AdminArticleAction.class).setActionMethod("editInit");
		amArticleEditInit.setMethod(RequestMethod.GET);
		amArticleEditInit.addResult("ADMIN_ARTICLE_EDIT", VIEW_PREFIX + "admin/article_edit.jsp");
		nsAdmin.addAction(amArticleEditInit);

		// 新建文章 - 保存
		ActionMapping amArticleAdd = new ActionMapping();
		amArticleAdd.setPath("/article/add/save").setAction(AdminArticleAction.class).setActionMethod("add");
		amArticleAdd.setMethod(RequestMethod.POST);
		amArticleAdd.setActionForm(ArticleForm.class);
		amArticleAdd.addResult("ADMIN_ARTICLE_ADD", VIEW_PREFIX + "admin/article_add.jsp");
		Result artilceAddSuccessResult = new Result();
		artilceAddSuccessResult.setName("ADMIN_ARTICLE_LIST");
		artilceAddSuccessResult.setResultType(ResultType.REDIRECT_ACTION);
		amArticleAdd.addResult(artilceAddSuccessResult);
		nsAdmin.addAction(amArticleAdd);

		// 编辑文章 - 保存
		ActionMapping amArticleEdit = new ActionMapping();
		amArticleEdit.setPath("/article/edit/save").setAction(AdminArticleAction.class).setActionMethod("edit");
		amArticleEdit.setMethod(RequestMethod.POST);
		amArticleEdit.setActionForm(ArticleForm.class);
		amArticleEdit.addResult("ADMIN_ARTICLE_EDIT", VIEW_PREFIX + "admin/article_edit.jsp");
		Result artilceEditSuccessResult = new Result();
		artilceEditSuccessResult.setName("ADMIN_ARTICLE_LIST");
		artilceEditSuccessResult.setResultType(ResultType.REDIRECT_ACTION);
		amArticleEdit.addResult(artilceEditSuccessResult);
		nsAdmin.addAction(amArticleEdit);

		namespaces.put(nsAdmin.getPath(), nsAdmin);

		return namespaces;
	}

}
